package cn.tom.controller.adm;

import cn.tom.dao.ClzDao;
import cn.tom.dao.CourseDao;
import cn.tom.dao.TaskDao;
import cn.tom.dao.UserDao;
import cn.tom.entity.Clz;
import cn.tom.entity.Course;
import cn.tom.entity.Task;
import cn.tom.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/admtask")
public class AdmTaskController {
    @Autowired
    TaskDao taskDao;
    @Autowired
    UserDao userDao;
    @Autowired
    CourseDao courseDao;
    @Autowired
    ClzDao clzDao;
//    查询所有数据
    @GetMapping("/findAll")
    public String find(Model model){
        List<Task> tasks=taskDao.findAll();
        model.addAttribute("task",tasks);
        return "adm/logintask";
    }
    //    添加
    @GetMapping("/go2add")
    public String go2add(Model model){
        List<Task> tasks = taskDao.findAll();
        model.addAttribute("task",tasks);
        List<User> user = userDao.findByRole("tea");
        model.addAttribute("user",user);
        List<Clz> clz = clzDao.findAll();
        model.addAttribute("clz",clz);
        List<Course> course = courseDao.findAll();
        model.addAttribute("course",course);
        model.addAttribute("action","add");
        return "adm/logintask";
    }
    @RequestMapping("/add")
    public String add(Task task, Model model){
        System.out.println(task);
        int tas = taskDao.add(task);
        System.out.println(tas);

        List<Task> tasks=taskDao.findAll();
        model.addAttribute("task",tasks);
        return "adm/logintask";
    }

   @GetMapping("/go2update")
    public String go2update(Model model,int kid){
       List<Task> tasks = taskDao.findAll();
       model.addAttribute("task",tasks);
       List<User> user = userDao.findByRole("tea");
       model.addAttribute("user",user);
       List<Clz> clz = clzDao.findAll();
       model.addAttribute("clz",clz);
       List<Course> course = courseDao.findAll();
       model.addAttribute("course",course);
       model.addAttribute("kid",kid);
        model.addAttribute("action","update");
        return "adm/logintask";
    }
    @RequestMapping("/update")
    public String update(Task task, Model model){
        System.out.println(task);
        int tas = taskDao.add(task);
        System.out.println(tas);

        List<Task> tasks=taskDao.findAll();
        model.addAttribute("task",tasks);
        return "adm/logintask";
    }

//删除
    @RequestMapping("/del")
    public String delete(int kid){
        int teask = taskDao.del(kid);
        System.out.println(teask);
        return "adm/logintask";
    }
}
